package com.study.flowable.springboot.controller;


import com.study.flowable.springboot.entity.ApproveVO;
import com.study.flowable.springboot.service.WorkFlowTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

/**
 * @author sjw
 * @description
 * @date 2019-08-31 下午3:12
 */

@RestController
@RequestMapping("/workFlow/task")
public class WorkFlowTaskController {


    @Autowired
    private WorkFlowTaskService workFlowTaskService;


    /**
     * 部署流程定义
     * @param name 流程名称
     * @param file bpmn 文件
     * @return
     * @throws IOException
     */
    @PostMapping("/deploy/{name}")
    public String deploy(@PathVariable String name, @RequestParam("filename") MultipartFile file) throws IOException {
        return workFlowTaskService.deploy(name,file);
    }

    /**
     * 流程列表
     * @return
     */
    @GetMapping("/deploy")
    public List<Map> deploy() {
        return workFlowTaskService.process();
    }


    /**
     * 启动流程
     * @param processId 流程id
     * @param variables 流程变量
     * @return
     */
    @PostMapping("/process/{processId}")
    public String process(@PathVariable String processId, @RequestBody Map<String,Object> variables){
        return workFlowTaskService.startProcess(processId,variables);
    }


    /**
     * 任务列表
     * @return
     */

    @GetMapping("/running")
    public List<Map> running() {
        return workFlowTaskService.running();
    }

    /**
     * 审批
     * @param approveVO
     */
    @PostMapping("/approveTask")
    public void approveTask(@RequestBody ApproveVO approveVO) {
        workFlowTaskService.approve(approveVO);
    }

    @GetMapping(value = "/processDiagram/{processId}")
    public void genProcessDiagram(@PathVariable String processId, HttpServletResponse httpServletResponse ) throws Exception {

        if(StringUtils.isEmpty(processId))return;

        workFlowTaskService.genProcessDiagram(processId,httpServletResponse);
    }

}
